<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <link rel="stylesheet" href="http://blog.aigouzhushou.com/layui-v2.4.3/layui/css/layui.css" media="all">
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="http://blog.aigouzhushou.com/layui-v2.4.3/layui/layui.js"></script>
<h1>欢迎登录resume系统！</h1><br/>
<h2>我是服务器：${pageContext.request.localPort}</h2>
<h2>当前sessionId：${pageContext.session.id}</h2>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="add()">
            <i class="layui-icon">&#xe654;</i> 新增
        </button>
    </div>
</script>
<table id="demo" lay-filter="demo"></table>
<script type="text/html" id="dataTableBar">
    <button class="layui-btn-normal layui-btn layui-btn-sm" lay-event="edit">编辑</button>
    <button class="layui-btn-danger layui-btn layui-btn-sm" lay-event="del">删除</button>
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        resumeTable = table.render({
            elem: '#demo',
            id: 'resumeTable',
            height: 312,
            url: '/resume/all', //数据接口
            toolbar: '#toolbarDemo',
            cols: [[ //表头
                {field: 'id', title: 'ID', sort: true, fixed: 'left'},
                {field: 'name', title: '姓名',},
                {field: 'address', title: '地址', sort: true},
                {field: 'phone', title: '联系方式', sort: true},
                {title: '操作', toolbar: '#dataTableBar', sort: true}
            ]]
        });

        //监听行工具事件
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除 “ ' + data.name + ' ” ？', function (index) {
                    $.ajax({
                        type: "POST",
                        url: '/resume/del',
                        data: {id: data.id},
                        error: function () {
                        },
                        success: function (result) {
                            if (result.code === 0) {
                                parent.layer.closeAll();
                                parent.layer.msg(result.msg);
                                table.reload('resumeTable', {
                                    url: '/resume/all'
                                });
                            } else {
                                parent.layer.alert(result.msg);
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                parent.layer.open({
                    title: '编辑',
                    type: 2,
                    maxmin: true,
                    content: '/resume/edit?id=' + data.id,
                    area: ['600px', '400px'],
                    btn: ['确定', '取消'],
                    success: function (layero) {
                    },
                    yes: function (index, layero) {
                        var iframeWin = top.window[layero.find('iframe')[0]['name']];
                        var data = iframeWin.getData();
                        if (data != null) {
                            $.ajax({
                                type: "POST",
                                url: '/resume/update',
                                data: data,
                                async: true,
                                error: function (request) {
                                    parent.layer.msg('请求失败');
                                },
                                success: function (result) {
                                    if (result.code === 0) {
                                        parent.layer.closeAll();
                                        parent.layer.msg(result.msg);
                                        table.reload('resumeTable', {
                                            url: '/resume/all'
                                        });
                                    } else {
                                        parent.layer.alert(result.msg);
                                    }
                                }
                            });
                        }
                    }
                });
            } else {

            }
        });
    });

    function add() {
        parent.layer.open({
            title: '添加',
            type: 2,
            maxmin: true,
            content: '/resume/add',
            area: ['600px', '400px'],
            btn: ['确定', '取消'],
            success: function (layero) {
            },
            yes: function (index, layero) {
                var iframeWin = top.window[layero.find('iframe')[0]['name']];
                var data = iframeWin.getData();
                if (data != null) {
                    $.ajax({
                        type: "POST",
                        url: '/resume/save',
                        data: data,
                        async: true,
                        error: function (request) {
                            parent.layer.msg('请求失败');
                        },
                        success: function (result) {
                            if (result.code === 0) {
                                parent.layer.closeAll();
                                parent.layer.msg(result.msg);
                                query();
                            } else {
                                parent.layer.alert(result.msg);
                            }
                        }
                    });
                }
            }
        });
    }

    function query() {
        resumeTable.reload();
    }
</script>
</body>
</html>
